package evi.com.evi.application;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.app.ActivityManager;
import android.app.ActivityManager.RunningAppProcessInfo;
import android.app.Application;
import android.content.Context;
import android.text.TextUtils;
import android.util.Log;

import com.tencent.bugly.Bugly;
import com.tencent.bugly.crashreport.CrashReport;

import cn.jpush.android.api.JPushInterface;
import evi.com.evi.utils.SPUtil;

/**
 * @作者： neil.chen
 * @创建时间： 2016年4月11日上午11:49:28
 * @描述: 全局数据与方法处理：存储 传递
 */
public class MyApplication extends Application {
    //公共数据区
    private static MyApplication mApplication;    //application引用
    private List<Activity> list;    //activity：管理
    private final String APP_ID = "c26f98b823";
    private static Context context;

    @Override
    public void onCreate() {
        super.onCreate();
        if (!isAppProcess()) return;    //Remote process does not require initialization
        if (mApplication == null)
            mApplication = this;
        list = new ArrayList<Activity>();


        // 注册默认的未捕捉异常处理类
        CrashHandler.getInstance(this);
        context = getApplicationContext();// 获取当前包名
        String packageName = context.getPackageName();// 获取当前进程名
        String processName = getProcessName(android.os.Process.myPid());// 设置是否为上报进程
        CrashReport.UserStrategy strategy = new CrashReport.UserStrategy(context);
        strategy.setUploadProcess(processName == null || processName.equals(packageName));
// 初始化Bugly
//        CrashReport.initCrashReport(context, APP_ID, false, strategy);
        Bugly.init(getApplicationContext(), APP_ID, false);


        Log.e("EE", "登录成功，开始启动极光推送");
        //启动极光推送
        JPushInterface.setDebugMode(true);
        JPushInterface.init(MyApplication.getInstance());
        String rid = JPushInterface.getRegistrationID(MyApplication.getContext());
        SPUtil.setString(MyApplication.getContext(), "channelId", rid);
        Log.d("EE", "极光rid=" + rid);
        //启动极光结束
    }

    /**
     * 获取进程号对应的进程名
     *
     * @param pid 进程号
     * @return 进程名
     */
    private static String getProcessName(int pid) {
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new FileReader("/proc/" + pid + "/cmdline"));
            String processName = reader.readLine();
            if (!TextUtils.isEmpty(processName)) {
                processName = processName.trim();
            }
            return processName;
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        } finally {
            try {
                if (reader != null) {
                    reader.close();
                }
            } catch (IOException exception) {
                exception.printStackTrace();
            }
        }
        return null;
    }

    //公共方法区:全局属性管理 ----------------------------------------------------------------

    /**
     * TODO	返回application实例
     */
    public static MyApplication getInstance() {
        return mApplication;
    }

    /**
     * TODO	注册activity
     */
    public void addActivity(Activity a) {
        list.add(a);
    }

    public void deleteActivity(Activity a) {
        list.remove(a);
    }

    public Activity getActivity() {
        return list.get(0);
    }

    /**
     * 退出所有登记activity
     */
    public void exitAllActivity() {
        for (Activity a : list) a.finish();
    }


    // 私有方法区:服务业务 ----------------------------------------------------------------

    /**
     * TODO return true if is xm app process,or false
     */
    private boolean isAppProcess() {
        String myProcessname = getPackageName();        //myapp process name

        int myPid = android.os.Process.myPid();        //current process flag
        List<RunningAppProcessInfo> apps = ((ActivityManager) getSystemService(Context.ACTIVITY_SERVICE)).getRunningAppProcesses();
        if (apps != null)
            for (RunningAppProcessInfo procInfo : apps) {
                if (procInfo.pid == myPid)
                    return myProcessname.equals(procInfo.processName);//匹配当前进程
            }
        return false;
    }

    public static Context getContext() {
        return context;
    }
}
